package com.liik.bomb.screen.action;


import com.liik.game.mvc.tiled.object.Entity;
import com.liik.game.mvc.tiled.object.MovableEntity;
import com.liik.game.mvc.tiled.object.Tile;
import com.liik.game.mvc.tiled.object.Tile.Type;
import com.liik.game.mvc.tiled.TiledGameModel;
import com.liik.game.mvc.tiled.TiledGameModel.Environment;
import com.liik.game.util.Vector;

public class EnemyEntity extends MovableEntity {
	
	public static final int NOT_SET = -1;
	private boolean isAlive;
	public long deathTime;
	private Vector direction;
	private Vector movementHelper;
	
	public EnemyEntity() {
		super(.1f, .4f, 20);
		isAlive = false;
		direction = new Vector(0, 0);
		movementHelper = new Vector();
		deathTime = NOT_SET;
	}
	
	public void initialize(float posX, float posY, float dirX, float dirY) {
		x = posX;
		y = posY;
		direction.set(dirX, dirY);
		deathTime = NOT_SET;
		isAlive = true;
	}

	@Override
	public boolean canWalk(Tile tile) {
		return tile.type == Type.Empty;
	}

	@Override
	public boolean canMove() {
		return isAlive;
	}

	@Override
	public boolean isActive() {
		return isAlive;
	}

	@Override
	public void update(TiledGameModel gameModel, long currentTimeMillis) {
		if (deathTime == NOT_SET) {
			boolean blockedByOtherEntity = isBlockedByOtherEntity(gameModel, this);
			movementHelper.set(this);
			movementHelper.add(direction);
			if (isValidLocation(gameModel, movementHelper) && (!isBlockedByOtherEntity(gameModel, movementHelper) || blockedByOtherEntity))
				this.set(movementHelper);
			else {
				direction.left();
			}
		} else if (deathTime < currentTimeMillis) {
			isAlive = false;
		}		
	}

	@Override
	public void handleCollision(Entity otherEntity, TiledGameModel gameModel, long currentTimeMillis) {
	}

	@Override
	public void handleEnvironment(Environment environment, TiledGameModel gameModel, long currentTimeMillis) {
		switch(environment) {
		case Air:
			break;
		case Fire:
			if (deathTime == NOT_SET) {
				deathTime = currentTimeMillis + 1000;
			}
			break;
		case Water:
			break;
		}
	}

	@Override
	protected boolean isBlockedByEntity(Entity entity) {
		return entity instanceof BombEntity;
	}

	@Override
	public void handleDefaultAction(TiledGameModel gameModel, long currentTimeMillis) {
		// TODO Auto-generated method stub
		
	}
}
